package com.agritech.task;

import com.agritech.machine.AgriculturalMachine;
import com.agritech.path.PathPlan;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 任务类
 * 表示一个分配给农机的具体任务
 */
public class Task {
    private String taskId;
    private String taskName;
    private String taskDescription;
    private AgriculturalMachine assignedMachine; // 分配的农机
    private PathPlan pathPlan; // 任务路径规划
    private TaskStatus status; // 任务状态
    private LocalDateTime creationTime; // 任务创建时间
    private LocalDateTime startTime; // 任务开始时间
    private LocalDateTime endTime; // 任务结束时间
    private double estimatedDuration; // 预计时长（小时）
    private double actualDuration; // 实际时长（小时）

    public enum TaskStatus {
        PENDING("待处理"),
        ASSIGNED("已分配"),
        IN_PROGRESS("进行中"),
        COMPLETED("已完成"),
        FAILED("失败"),
        CANCELLED("已取消");

        private final String description;

        TaskStatus(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    public Task(String taskId, String taskName, String taskDescription, double estimatedDuration) {
        this.taskId = taskId;
        this.taskName = taskName;
        this.taskDescription = taskDescription;
        this.estimatedDuration = estimatedDuration;
        this.status = TaskStatus.PENDING;
        this.creationTime = LocalDateTime.now();
    }

    /**
     * 分配任务给农机
     * @param machine 农机
     * @param pathPlan 路径规划
     */
    public void assignToMachine(AgriculturalMachine machine, PathPlan pathPlan) {
        this.assignedMachine = machine;
        this.pathPlan = pathPlan;
        this.status = TaskStatus.ASSIGNED;
        System.out.println("任务 '" + taskName + "' 已分配给农机: " + machine.getId() + " (" + machine.getType() + ")");
    }

    /**
     * 开始任务
     */
    public void startTask() {
        if (status == TaskStatus.ASSIGNED && assignedMachine != null) {
            this.status = TaskStatus.IN_PROGRESS;
            this.startTime = LocalDateTime.now();
            System.out.println("任务 '" + taskName + "' 开始执行");
            assignedMachine.performTask(taskDescription);
            if (pathPlan != null) {
                System.out.println("农机将按照以下路径执行任务:");
                pathPlan.printPathPlan();
            }
        } else {
            System.out.println("任务 '" + taskName + "' 无法开始，请先分配农机或检查任务状态");
        }
    }

    /**
     * 完成任务
     */
    public void completeTask() {
        if (status == TaskStatus.IN_PROGRESS) {
            this.status = TaskStatus.COMPLETED;
            this.endTime = LocalDateTime.now();
            this.actualDuration = java.time.Duration.between(startTime, endTime).toMillis() / (1000.0 * 60 * 60);
            System.out.println("任务 '" + taskName + "' 已完成，实际用时: " + String.format("%.2f", actualDuration) + " 小时");
        } else {
            System.out.println("任务 '" + taskName + "' 状态异常，无法标记为完成");
        }
    }

    /**
     * 取消任务
     */
    public void cancelTask() {
        if (status == TaskStatus.PENDING || status == TaskStatus.ASSIGNED || status == TaskStatus.IN_PROGRESS) {
            this.status = TaskStatus.CANCELLED;
            this.endTime = LocalDateTime.now();
            System.out.println("任务 '" + taskName + "' 已取消");
        } else {
            System.out.println("任务 '" + taskName + "' 状态为 '" + status.getDescription() + "'，无法取消");
        }
    }

    /**
     * 任务失败
     * @param reason 失败原因
     */
    public void failTask(String reason) {
        if (status == TaskStatus.IN_PROGRESS) {
            this.status = TaskStatus.FAILED;
            this.endTime = LocalDateTime.now();
            System.out.println("任务 '" + taskName + "' 执行失败: " + reason);
        } else {
            System.out.println("任务 '" + taskName + "' 状态异常，无法标记为失败");
        }
    }

    /**
     * 打印任务详情
     */
    public void printTaskDetails() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println("--- 任务详情 ---");
        System.out.println("任务ID: " + taskId);
        System.out.println("任务名称: " + taskName);
        System.out.println("任务描述: " + taskDescription);
        System.out.println("状态: " + status.getDescription());
        System.out.println("创建时间: " + creationTime.format(formatter));
        if (assignedMachine != null) {
            System.out.println("分配农机: " + assignedMachine.getId() + " (" + assignedMachine.getType() + ")");
        }
        if (startTime != null) {
            System.out.println("开始时间: " + startTime.format(formatter));
        }
        if (endTime != null) {
            System.out.println("结束时间: " + endTime.format(formatter));
        }
        System.out.println("预计时长: " + estimatedDuration + " 小时");
        if (status == TaskStatus.COMPLETED) {
            System.out.println("实际时长: " + String.format("%.2f", actualDuration) + " 小时");
        }
        if (pathPlan != null) {
            System.out.println("关联路径规划: " + pathPlan.getPlanningMethod());
        }
        System.out.println("----------------");
    }

    // Getters
    public String getTaskId() { return taskId; }
    public String getTaskName() { return taskName; }
    public String getTaskDescription() { return taskDescription; }
    public AgriculturalMachine getAssignedMachine() { return assignedMachine; }
    public PathPlan getPathPlan() { return pathPlan; }
    public TaskStatus getStatus() { return status; }
    public LocalDateTime getCreationTime() { return creationTime; }
    public LocalDateTime getStartTime() { return startTime; }
    public LocalDateTime getEndTime() { return endTime; }
    public double getEstimatedDuration() { return estimatedDuration; }
    public double getActualDuration() { return actualDuration; }
}